package com.rc.net;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URISyntaxException;
import java.net.URL;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.entity.BufferedHttpEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;

import android.util.Log;

import com.rc.util.Const;
import com.telpoo.frame.utils.Cons.NetConfig;

public class NetUtils implements NetConfig {

	public static boolean download(URL url, OutputStream outputStream) {

		HttpGet httpRequest = null;
		try {
			httpRequest = new HttpGet(url.toURI());
		} catch (URISyntaxException e) {
			Log.i("NAQ",
					"Downloader - SERVICE_JOB_COMMAND_DOWNLOAD_FILE - URISyntaxException e = "
							+ e);
			return false;
		}
		HttpParams params = new BasicHttpParams();
		HttpConnectionParams.setConnectionTimeout(params, CONNECTION_TIMEOUT);
		HttpConnectionParams.setSoTimeout(params, SO_TIMEOUT);
		HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
		HttpClient httpclient = new DefaultHttpClient(params);
		int numberOfRetry = NUMBER_OF_RETRY;
		while (numberOfRetry-- >= 0) {
			try {
				HttpResponse response = (HttpResponse) httpclient
						.execute(httpRequest);

				HttpEntity entity = response.getEntity();

				BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(
						entity);

				InputStream instream = bufHttpEntity.getContent();
				byte[] data = new byte[FILE_BUFFER_SIZE];
				int bytesRead = 0;
				while ((bytesRead = instream.read(data)) > 0) {
					outputStream.write(data, 0, bytesRead);
				}
				return true;
			} catch (Exception e) {
				Log.i("nth",
						"Downloader - SERVICE_JOB_COMMAND_DOWNLOAD_FILE - Exception e = "
								+ e);
				return false;
			}
		}
		return false;
	}

	@SuppressWarnings("resource")
	public static boolean DownloadImageFromPath(String path, String mNameFile) {
		InputStream in = null;
		int responseCode = -1;
		try {

			URL url = new URL(path);
			HttpURLConnection con = (HttpURLConnection) url.openConnection();
			con.setDoInput(true);
			con.connect();
			responseCode = con.getResponseCode();
			if (responseCode == HttpURLConnection.HTTP_OK) {
				// download
				in = con.getInputStream();
				mNameFile = mNameFile + ".pdf";
				File mFile = new File(Const.FILE_KEY + mNameFile);
				mFile.deleteOnExit();
				FileOutputStream fOut = new FileOutputStream(mFile);
				try {

					fOut = new FileOutputStream(mFile);
					int c;

					while ((c = in.read()) != -1) {
						fOut.write(c);
					}

				} finally {
					if (in != null) {
						in.close();
					}
					if (fOut != null) {
						fOut.close();
					}
				}
			} else {
				return false;
			}

		} catch (Exception ex) {
			Log.e("Exception", ex.toString());
			ex.printStackTrace();
			return false;
		}
		return true;
	}

}